<?php
namespace OxyBase\BoundedContexts\BoundedContextA\Domain\FirstModule\AggregateRoots;

use Oxy\Domain\AggregateRoot\EventSourcedAbstract;

class Account extends EventSourcedAbstract
{        
    /**
     * Initialize aggregate root
     * 
     * @param Oxy_Guid $guid
     * @param string $realIdntifier
     */
    public function __construct(
        Oxy_Guid $guid, 
        $realIdntifier
    )
    {
        parent::__construct($guid, $realIdntifier);
    }
                   
    /**
     */
    public function addProduct(
    )
    {
	    $this->_handleEvent(
		new Account_Domain_Account_AggregateRoot_Account_Event_NewAccountCreated(
		    array(
			'accountGuid' => (string)$this->_guid,
			'primaryEmail' => (string)$primaryEmailAddress,
			'password' => (string)$password,
			'passwordAgain' => (string)$passwordAgain,
			'isAutoGenerated' => (boolean)$password->isAutoGenerated(),
			'encodedPassword' => $encodedPassword,
			'personalInformation' => $ownerPersonalInformation->toArray(),
			'deliveryInformation' => $ownerDeliveryInformation->toArray(),
			'settings' => $settings->getProperties(),
			'state' => Account_Domain_Account_ValueObject_State::ACCOUNT_STATE_INITIALIZED,
			'emailActivationKey' => (string)$emailActivationKey,
		    	'date' => date('Y-m-d H:i:s'),
		    	'loginState' => (string)$this->_loginState,
		    	'generatedPassword' => $generatedPassword,
		    )
		)
	    );  

	    $this->_throwWrongStateException('MSA::Account::confirmPrimaryEmailAddress', $this->_state);
    }
        
    /**
     * @param Account_Domain_Account_AggregateRoot_Account_Event_NewAccountCreated $event
     */
    protected function onNewAccountCreated(
        Account_Domain_Account_AggregateRoot_Account_Event_NewAccountCreated $event
    )
    {
        $this->_primaryEmail = new Account_Domain_Account_ValueObject_EmailAddress(
            $event->getPrimaryEmail()
        );
        
        $this->_state = new Account_Domain_Account_ValueObject_State(
            $event->getState()
        );    
        $this->_currentPassword = new Account_Domain_Account_ValueObject_Password(
            $event->getEncodedPassword(),
            true,
            $event->getIsAutoGenerated()
        );     
        
        $this->_activationKey = new Oxy_Guid($event->getEmailActivationKey());
        $this->_settings = new Account_Domain_Account_ValueObject_Properties($event->getSettings());
    }
    
    /**
     * Create snapshot
     *
     * @return Oxy_EventStore_Storage_Memento_MementoInterface
     */
    public function createMemento()
    {   
        $memento = array(
            'eventName' => 'MementoCreated',
        );
          
        return new Account_Domain_Account_AggregateRoot_Account_Memento_Account(
            $memento
        );
    }
        
    /**
     * Load snapshot
     *
     * @param Oxy_EventStore_Storage_Memento_MementoInterface $memento
     * @return void
     */
    public function setMemento(Oxy_EventStore_Storage_Memento_MementoInterface $memento)
    {
        $this->_guid = new Oxy_Guid($memento->getAccountGuid());
    }
}
